/*
 * Licensed this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 * AUTHOR: Sebastian Ritter
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace org.boapi.shop {
  /// <summary>
  /// International Standard Serial Number is a 8 digits number for periodic papers like journals.
  /// </summary>
  /// <version>2.0.0.0</version>
  public sealed class ISSN : org.boapi.core.IChecknumber {
    /// <summary>
    /// Creates a ISSN Object from EAN
    /// </summary>
    /// <param name="ean"></param>
    /// <returns></returns>
    public static ISSN parse (EAN ean) {
      if (!ean.getNumber ().ToString ().StartsWith ("977")) {
        throw new ArgumentException ("EAN13 is not an ISSN, because it starts not with 977", "ean");
      }
      // EAN = 977072486790p => 0724-8679
      ISSN instance = new ISSN ();
      String value = ean.getNumber ().ToString ().Substring (3, 8);
      instance.setCompleteValue (value);
      instance.isChecknumberCorrect ();
      value = value.Substring (0, value.Length - 1) + instance.getChecknumber ();
      instance.setCompleteValue (value);
      return instance;
    }
    /// <summary>
    /// International Standard Serial Number
    /// </summary>
    private String number;
    /// <summary>
    /// Set the ISSN
    /// </summary>
    /// <param name="issn"></param>
    public void setCompleteValue (params string[] issn) {
      StringBuilder nummer = new StringBuilder ();
      foreach (Char c in issn[0].ToUpper().ToCharArray ()) {
        if (Char.IsDigit (c) || c == 'X') {
          nummer.Append (c);
        }
        else if (c != '-') {
          throw new ArgumentException ("Illegal character in ISSN", "issn");
        }
      }
      while (nummer.Length < 8) {
        nummer.Insert (0,'0');
      }
      number = nummer.ToString ();
      this.checknumber = 'R';
    }
    /// <summary>
    /// The number of ISSN
    /// </summary>
    /// <returns></returns>
    public String getNumber () {
      return this.number;
    }

    /// <summary>
    /// The valid checknumber
    /// </summary>
    private char checknumber = 'R';
    /// <summary>
    /// Return the valid checknumber
    /// </summary>
    /// <returns></returns>
    private char getChecknumber () {
      if (this.checknumber == 'R') {
        this.isChecknumberCorrect ();
      }
      return this.checknumber;
    }

    /// <summary>
    /// Is the checknumber correct
    /// </summary>
    /// <returns></returns>
    public bool isChecknumberCorrect () {
      int[] factors = new int[] { 8, 7, 6, 5, 4, 3, 2 }; // only first seven
      int[] digits = new int[factors.Length]; 
      for (int i = 0; i < digits.Length; i++) {
        digits[i] = int.Parse(this.number.Substring (i, 1));
      }
      int sum = 0;
      for (int i = 0; i < digits.Length; i++) {
        sum += digits[i] * factors[i];
      }
      int mod11 = sum % 11;
      int checkDigit = 11 - mod11;
      switch (checkDigit) {
        case 10: 
          this.checknumber = 'X';
          break;
        case 11:
          this.checknumber = '0';
          break;
        default:
          this.checknumber = char.Parse (checkDigit.ToString());
          break;
      }
      return this.checknumber == this.number.ToCharArray(7,1)[0];
    }
  }
}
